var that = '';
Page({
  data: {
    box_height:900,
    imgB64: '',
    flag:true,
    current: 0,
    swiper_img: ['/images/2.jpg', '/images/1.jpg', '/images/3.jpg'],
    title: '菊花',
    artist: '唐·元稹',
    content: {
      one: '秋丛绕舍似陶家',
      two: '遍绕篱边日渐斜',
      three: '不是花中偏爱菊',
      four: '此花开尽更无花'
    },
    baidu_img1:'',
    baidu_url:''
  },
  onLoad: function (options) {
    that = this;
  },
  /**
   * 选择图片
   */
  chooseimgTap: function () {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths[0];
        that.getB64ByUrl(tempFilePaths);
        that.setData({
          img: tempFilePaths
        });
        wx.showLoading({
          title: '正在识别中~',
          mask:true,
          success:res=>{
            console.log(res)
            // that.plantTap()
          },
          fail:err=>{
            console.log(err)
          }
        })
        setTimeout(function () {
          that.plantTap()
          wx.hideLoading()
        }, 5000)

      }
    })
  },
  /**
   * 转b64
   */
  getB64ByUrl: function (url) {
    console.log(url)
    const FileSystemManager = wx.getFileSystemManager();
    FileSystemManager.readFile({
      filePath: url,
      encoding: 'base64',
      success(res) {
        // console.log(res.data);
        that.setData({
          imgB64: res.data
        });
      }
    })
  },

  /**
   * 识别
   */
  plantTap: function (e) {
    const imgB64 = that.data.imgB64;
    if (!imgB64) {
      that.setData({
        ishow: true
      });
      return;
    };

    wx.showLoading({
      title: '识别中',
    })

    setTimeout(function () {
      wx.hideLoading()
    }, 300)



    that.getToken(function (token) {
      that.getResult(token);
    });
  },
  getToken: function (callback) {
    wx.request({
      url: 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=FT6vzMxsZEgdIAwa6CkEIPIK&client_secret=CBrtoKUsQBwL23e12tWztGmntj0mevoZ',
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success(res) {
        var token = res.data.access_token;
        console.log(token);

        return callback(token);
      }
    });
  },
  getResult: function (token) {
    wx.request({
      url: 'https://aip.baidubce.com/rest/2.0/image-classify/v1/plant?access_token=' + token,
      method: "post",
      data: {
        image: that.data.imgB64,
        baike_num:5,
      },

      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        console.log(res)
        console.log(res.data.result[0].name);
        if(res.data.result[0].name == "非植物"){
          wx.showModal({
            showCancel:false,
            confirmText:"确定",
            content:"此识别结果为非植物，请重新上传照片~"
          })
        }else{
          var name = []
          var num = []
          var score = []
          var baidu_image = []
          var introduce = res.data.result[0].baike_info.description
          var baidu_url = res.data.result[0].baike_info.baike_url
          for (let index = 0; index < res.data.result.length; index++) {
            name.push(res.data.result[index].name)
            num.push(res.data.result[index].score)
            baidu_image.push(res.data.result[index].baike_info.image_url)
          }
          for (let j = 0; j < num.length; j++) {
            score.push(100*num[j].toFixed(2))
          }
          console.log(score)
          that.setData({
            flag:false,
            box_height:1100,
            baidu_url:baidu_url,
            baidu_introduce:introduce,
            baidu_img1:baidu_image[0],
            baidu_img2:baidu_image[1],
            baidu_img3:baidu_image[2],
            name1:name[0],
            name2:name[1],
            name3:name[2],
            score1:score[0],
            score2:score[1],
            score3:score[2]
          })
        }
      }
    });
  },
  watch_more(){
    wx.navigateTo({
      url: '/pages/webViewPage/webViewPage?url='+this.data.baidu_url,
    })
  },
  end_change(e) {
    var id = e.detail.current
    var content = [{
      one: '秋丛绕舍似陶家',
      two: '遍绕篱边日渐斜',
      three: '不是花中偏爱菊',
      four: '此花开尽更无花'
    }, {
      one: '咬定青山不放松',
      two: '立根原在破岩中',
      three: '千磨万击还坚劲',
      four: '任尔东西南北风'
    },{
      one: '何人不爱牡丹花',
      two: '占断城中好物华',
      three: '疑是洛川神女作',
      four: '千娇万态破朝霞'
    }
  ]
    switch (id) {
      case 0:
        this.setData({
          title: '菊花',
          content: content[0],
          artist:"唐·元稹"
        })
        break;
      case 1:
        this.setData({
          title: "竹石",
          content: content[1],
          artist:'清·郑板桥'
        })
        break;
      case 2:
        this.setData({
          title: "牡丹",
          content: content[2],
          artist:'唐·徐凝'
        })
        break;
    }
  },
  back(){
    this.setData({
      flag:true,
      box_height:900
    })
  },
})